package overrides;

import java.awt.Color;
import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class formatoTabla extends DefaultTableCellRenderer {

    private final int columna_patron;
    DefaultTableModel dtm;
    private final DateFormat dateFormat;
    private final Date date;
    private final TableColumnModel tcm;

    public formatoTabla(int Colpatron, DefaultTableModel dt, JTable tabla) {
        columna_patron = Colpatron;
        dtm = dt;
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        date = new Date();
        tcm = tabla.getColumnModel();
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean focused, int row, int column) {
        java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, selected, focused, row, column);
        // cellComponent.setBackground(Color.WHITE);
        cellComponent.setForeground(Color.WHITE);
        if (value == null) {
            return cellComponent;
        }
        TableColumn columna = tcm.getColumn(column);

        /* if (table.isCellSelected(row, column)) {
         cellComponent.setBackground(Color.blue);
         } else */        
        if (table.isRowSelected(row)) {
            cellComponent.setForeground(Color.black);
            cellComponent.setBackground(new Color(150, 150,255));
        } else if (columna.getHeaderValue().toString().contains("Cantidad")) {
            if ((int) value <= 0) {
                cellComponent.setBackground(Color.RED);
            } else if ((int) value <= 3) {
                cellComponent.setForeground(Color.black);
                cellComponent.setBackground(Color.YELLOW);
            } else {
                cellComponent.setForeground(Color.black);
            }
        } else if (columna.getHeaderValue().equals("Fecha-Act")) {
            cellComponent.setForeground(Color.black);
            if (value != null) {
               // System.out.println("fecha:"+dateFormat.format(date)+" actualizacion:"+value.toString());
                if (!dateFormat.format(date).equals(value.toString())) {
                    cellComponent.setBackground(Color.YELLOW);
                 //   System.out.println("hoy");
                }
            }
        } else {
            cellComponent.setBackground(Color.white); //color de fondo
            cellComponent.setForeground(Color.black);//color de texto
        }

        super.getTableCellRendererComponent(table, value, selected, focused, row, column);
        return cellComponent;
    }

}
